//
//  AppDelegate.m
//  quickLook
//
//  Created by J on 15/8/30.
//  Copyright (c) 2015年 JZJ. All rights reserved.
//

#import "AppDelegate.h"
#import "Header.h"
#import "UMSocial.h"
#import "MainTabBarController.h"
#import "APService.h"
@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    _window.backgroundColor=JZJYELLOW;
    
    
    
    
    self.window.rootViewController=[[MainTabBarController alloc]init];
    
    
    //背景颜色
//    [[UINavigationBar appearance] setBarTintColor:JZJYELLOW];
    
    
    //文字颜色
    [[UINavigationBar appearance] setTintColor:JZJLABELCOLOR];
    
    
    //titlecolor 颜色
    [[UINavigationBar appearance]setTitleTextAttributes:@{NSForegroundColorAttributeName:JZJLABELCOLOR ,NSFontAttributeName:[UIFont boldSystemFontOfSize:18]}];

    [UMSocialConfig hiddenNotInstallPlatforms:@[UMShareToWechatSession,UMShareToWechatTimeline]];
    
    
    [UMSocialData setAppKey:@"5606db0ce0f55a58d1003a21"];

    NSUserDefaults* userdefault=[NSUserDefaults standardUserDefaults];

    NSMutableArray* idArray=[NSMutableArray array];
    
    [userdefault setObject:idArray forKey:IDArray];
    
    [self.window makeKeyAndVisible];
    
    [self connect];
    
    
    
    
    
//引导页
    
    
    
    //Required
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
        //可以添加自定义categories
        [APService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge |
                                                          UIUserNotificationTypeSound |
                                                          UIUserNotificationTypeAlert)
                                              categories:nil];
    } else {
        //categories 必须为nil
        [APService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
                                                          UIRemoteNotificationTypeSound |
                                                          UIRemoteNotificationTypeAlert)
                                              categories:nil];
    }
    //Required

    
    
    
    return YES;
}


#pragma mark 通知
-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
    [APService registerDeviceToken:deviceToken];
    NSString* originStr = [deviceToken description];
    NSString* tokenStr = [[originStr substringFromIndex:1] substringToIndex:originStr.length - 2];
    tokenStr = [tokenStr stringByReplacingOccurrencesOfString:@" " withString:@""];
    NSLog(@"设备注册成功，\n%@",tokenStr);
}
-(void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
    NSLog(@"设备注册失败，%@",error);
}

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    NSLog(@"%@",userInfo);
    [APService handleRemoteNotification:userInfo];
}

- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void
                        (^)(UIBackgroundFetchResult))completionHandler {

}

- (void)connect{
    
    
    
    
    //<1>初始化请求队列管理者对象
    AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];
    //<2>
    //-----------扩展---------
    //判断当前网络状态
    /*
     AFNetworkReachabilityStatusNotReachable     = 0,
     AFNetworkReachabilityStatusReachableViaWWAN = 1,
     AFNetworkReachabilityStatusReachableViaWiFi = 2,
     */
    
    NSUserDefaults* userdefault= [NSUserDefaults standardUserDefaults];

    [manager.reachabilityManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        if(status == AFNetworkReachabilityStatusReachableViaWiFi)
        {
            
            JZJLog(@"appdelegate WIFI");
            
            
            [userdefault setBool:YES forKey:ISCONNECTED];
            
            [userdefault synchronize];
//            [ProgressHUD showError:@"当前网络切换为WIFI" Interaction:YES];
            [NSTimer scheduledTimerWithTimeInterval:.0 target:self selector:@selector(dismiss) userInfo:0 repeats:NO];

        }
        else if (status == AFNetworkReachabilityStatusReachableViaWWAN)
        {
            
            
            [userdefault setBool:YES forKey:ISCONNECTED];
            
            [userdefault synchronize];

            JZJLog(@"3G/GPRS");
        }
        else
        {
            [userdefault setBool:NO forKey:ISCONNECTED];
            [userdefault synchronize];

            JZJLog(@"无网络连接");
        }
    }];
    //开启网络监控
    [manager.reachabilityManager startMonitoring];
    
    //只要当前网络发生改变 就会立即调用block语句块
    
    
    [NSThread sleepForTimeInterval:0.5];


}

- (void)dismiss{
    
    [ProgressHUD dismiss];
}


- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
